package com.freesky.biz.activity.service.impl;

import com.freesky.biz.activity.domain.*;
import com.freesky.biz.activity.mapper.CouponBillMapper;
import com.freesky.biz.activity.mapper.CouponDetailMapper;
import com.freesky.biz.activity.mapper.StockMonitorMapper;
import com.freesky.biz.activity.service.ICouponBillService;
import com.freesky.biz.activity.service.ICouponDetailService;
import com.freesky.biz.activity.service.IStockMonitorService;
import com.freesky.biz.pay.service.ICouponStockService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CouponBillServiceImpl implements ICouponBillService {

    @Autowired
    private CouponBillMapper couponBillMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TCouponBill> findList(TCouponBill entity) {
        return couponBillMapper.findList(entity);
    }

    @Override
    public TStockMonitorSummary selectSummaryData(TCouponBill entity) {
        return couponBillMapper.selectSummaryData(entity);
    }

    @Override
    public List<TCouponBillSummary> selectBillSummaryList(TCouponBill entity) {
        return couponBillMapper.selectBillSummaryList(entity);
    }

    @Override
    public AjaxResult exportSummaryFile(TCouponBill entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("COUPON_BILL_SUMMARY");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TCouponBillSummary(), ICouponBillService.class,
                "selectBillSummaryList", "核销明细汇总",Long.valueOf(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public AjaxResult exportFile(TCouponBill entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("COUPON_BILL");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TCouponBill(), ICouponBillService.class,
                "findList", "核销明细（微信）",Long.valueOf(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public int deleteBillByConsumeTime(String stockId, String startDt,String endDt){
        return couponBillMapper.deleteBillByConsumeTime(stockId,startDt,endDt);
    }

    @Override
    public int addBatchOrUpdate(List<TCouponBill> couponBills) {
        return couponBillMapper.addBatchOrUpdate(couponBills);
    }

    @Override
    public int updateEmptyBankNo(String stockId, String consumeTime) {
        return couponBillMapper.updateEmptyBankNo(stockId,consumeTime);
    }

    @Override
    public String selectMaxUpdateDate() {
        return couponBillMapper.selectMaxUpdateDate();
    }
}
